/*
 * Copyright (C) 2015 The Android  Source Project 
 *
 * Licensed under the Apache License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at 
 *
 *      http://www.ido517.com
 *
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 */
package utils;

import java.io.File;

import com.ygk.bluepath.core.BActivity;
import com.ygk.bluepath.core.BApp;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.Point;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Environment;
import android.telephony.TelephonyManager;
import android.view.Display;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

/**
 * @Description TODO
 * @author 李文伟
 * @email lvsedewenzi@163.com
 * @date 2015-6-18 上午10:24:37
 */
@SuppressLint("NewApi") 
public class PhoneInformationUtil {

	private static BApp app = BApp.getInstance();
	private static TelephonyManager telphony = (TelephonyManager) app
			.getSystemService(Context.TELEPHONY_SERVICE);

	private static ConnectivityManager connectMgr = (ConnectivityManager) app
			.getSystemService(Context.CONNECTIVITY_SERVICE);

	private static NetworkInfo networkInfo = connectMgr.getActiveNetworkInfo();

	// ------------------------------------------------------------------------------------------------
	public static String getimei() {
		String imei = telphony.getDeviceId();
		if (imei == null || imei.isEmpty()) {
			return "";
		}
		return imei;
	}

	@TargetApi(13)
	@SuppressWarnings("deprecation")
	public static String getscreen(BActivity c) {

		Display display = c.getWindowManager().getDefaultDisplay();
		Point size = new Point();
		int width = 0;
		int height = 0;
		if (android.os.Build.VERSION.SDK_INT >= 13) {
			display.getSize(size);
			width = size.x;
			height = size.y;
		} else if (android.os.Build.VERSION.SDK_INT < 13) {

			width = display.getWidth();
			height = display.getHeight();
		}

		c.sp.setScreen_w(width);
		c.sp.setScreen_h(height);
		String str = width + "x" + height;
		// System.out.println("Tool-------"+width+"x"+height) ;

		return str;
	}

	public static String getnetwork_state() {

		String network = "";

		if (networkInfo != null) {

			if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
				network = "1";
			} else if (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {

				switch (telphony.getNetworkType()) {
				case TelephonyManager.NETWORK_TYPE_EDGE:
					network = "2";
					break;
				case TelephonyManager.NETWORK_TYPE_GPRS:
					network = "2";
					break;
				case TelephonyManager.NETWORK_TYPE_CDMA:
					network = "2";
					break;
				case TelephonyManager.NETWORK_TYPE_1xRTT:
					network = "2";
					break;
				case TelephonyManager.NETWORK_TYPE_IDEN:
					network = "2";
					break;// ~25 kbps
				case TelephonyManager.NETWORK_TYPE_HSDPA:
					network = "3";
					break; // ~ 2-14 Mbps
				case TelephonyManager.NETWORK_TYPE_HSPA:
					network = "3";
					break; // ~ 700-1700 kbps
				case TelephonyManager.NETWORK_TYPE_HSUPA:
					network = "3";
					break; // ~ 1-23 Mbps
				case TelephonyManager.NETWORK_TYPE_UMTS:
					network = "3";
					break; // ~ 400-7000 kbps
				case TelephonyManager.NETWORK_TYPE_EHRPD:
					network = "3";
					break; // ~ 1-2 Mbps
				case TelephonyManager.NETWORK_TYPE_EVDO_0:
					network = "3";
					break;
				case TelephonyManager.NETWORK_TYPE_EVDO_A:
					network = "3";
					break;
				case TelephonyManager.NETWORK_TYPE_EVDO_B:
					network = "3";
					break; // ~ 5 Mbps
				case TelephonyManager.NETWORK_TYPE_HSPAP:
					network = "3";
					break; // ~ 10-20 Mbps
				case TelephonyManager.NETWORK_TYPE_LTE:
					network = "4";
					break; // ~ 10+ Mbps
				case TelephonyManager.NETWORK_TYPE_UNKNOWN:
					network = "0";
					break;
				default:
					network = "0";
					break;
				}
			}

		} else {
			network = "0";
		}

		return network;

	}

	public static String getbrand() {
		String brand = "";
		brand = android.os.Build.BRAND;
		return brand;
	}

	public static String getsys() {
		String sys = "";
		sys = android.os.Build.VERSION.RELEASE;
		return sys;
	}





	/*
	 * 2. * 获取当前程序的版本号 3.
	 */
	public static String getVersionName() {
		// 获取packagemanager的实例
		PackageManager packageManager = app.getPackageManager();
		// getPackageName()是你当前类的包名，0代表是获取版本信息
		PackageInfo packInfo;
		try {
			packInfo = packageManager.getPackageInfo(app.getPackageName(), 0);

			System.out.println(packInfo.versionName + "----获取版本号！");
			return packInfo.versionName;
		} catch (NameNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();

			return "";
		}

	}

	/*
	 * 2. * 获取当前程序的版本号 3.
	 */
	public static int getVersionCode() {
		// 获取packagemanager的实例
		PackageManager packageManager = app.getPackageManager();
		// getPackageName()是你当前类的包名，0代表是获取版本信息
		PackageInfo packInfo;
		try {
			packInfo = packageManager.getPackageInfo(app.getPackageName(), 0);

			System.out.println(packInfo.versionCode + "----获取版本代码！");
			return packInfo.versionCode;
		} catch (NameNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();

			return 0;
		}

	}

	// 安装apk
	public static void installApk(File file, Context mContext) {

		if (file != null) {
			Intent intent = new Intent(Intent.ACTION_VIEW);

			// 执行的数据类型
			intent.setDataAndType(Uri.fromFile(file),
					"application/vnd.android.package-archive");
			// intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
			// Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
			mContext.startActivity(intent);
		}

	}

	public static boolean isNetworkAvailable() {
		ConnectivityManager mgr = (ConnectivityManager) app
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo[] info = mgr.getAllNetworkInfo();
		if (info != null) {
			for (int i = 0; i < info.length; i++) {
				if (info[i].getState() == NetworkInfo.State.CONNECTED) {
					return true;
				}
			}
		}
		return false;
	}
	public static boolean hasSdCard() {
		// TODO Auto-generated method stub
		String state = Environment.getExternalStorageState();
		if (state.equals(Environment.MEDIA_MOUNTED)) {
			return true;
		} else {
			//tt("未检测到存储卡");
			return false;
		}

	}

/*	public static void hideKeyBoard(Context mContext,View view) {
		InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
		//imm.showSoftInput(view,InputMethodManager.SHOW_FORCED);
		imm.hideSoftInputFromWindow(view.getWindowToken(), 0); //强制隐藏键盘
	}*/
}